<?php
/* 
-------------------------------
ERP Elyazalée 
Développé par Loïc PIQUARD
SARL Elyazalée - 2009
www.elyazalee.com
-------------------------------
*/
if ($_GET['post']==3 and $droit_supprimer==1)
	{
	// supprimer la vignette	
	$requete=requete("SELECT vignette_orig, vignette_mini FROM actualites WHERE id='".mysql_real_escape_string($_GET['id'])."'");
	$result=mysql_fetch_array($requete);
	$vignette_orig=$result['vignette_orig'];
	$vignette_mini=$result['vignette_mini'];
	if (strlen($vignette_orig)>3)
		{
		@unlink($chemin_relatif."/upload_galeries/".$vignette_orig);
		@unlink($chemin_relatif."/upload_galeries/".$vignette_mini);
		}
	$requete=requete("UPDATE actualites SET
					 				vignette_orig='',
									vignette_mini=''
						 WHERE id='".mysql_real_escape_string($_GET['id'])."'");
		
	logs($numero_module, $_GET['id'], "Suppression d'une vignette d'une actualit&eacute; (".mysql_real_escape_string($_FILES['fichier']["name"]).")");
	affiche_message("La vignette a &eacute;t&eacute; supprim&eacute; !");
	}

if ($_POST['post']==2 and $droit_editer==1)
	{
	// ajout vignette
	// recup : taille (x;y) , fichier
	$taille=explode(";",$_POST['taille']);
	
	if (is_uploaded_file($_FILES['fichier']['tmp_name'])) 
		{
		if ($_FILES['fichier']["type"]=="image/png" OR $_FILES['fichier']["type"]=="image/gif" OR $_FILES['fichier']["type"]=="image/jpg" OR $_FILES['fichier']["type"]=="image/jpeg" OR $_FILES['fichier']["type"]=="image/pjpeg")
			{
			// image
			$boutfichier=explode(".",$_FILES['fichier']["name"]);
			$nbbout=count($boutfichier);
			$nomtemporaire=mdp(15).".".$boutfichier[$nbbout-1];				
			move_uploaded_file($_FILES['fichier']['tmp_name'],$chemin_relatif."/upload_galeries/".$nomtemporaire);
		
			
			trans_image_coupe($nomtemporaire,"vignette_".$nomtemporaire,"","0,0,0",10,$taille[0],0,60);
		
			// verifie si une vignette existait déjà :
			
			$requete=requete("SELECT vignette_orig, vignette_mini FROM actualites WHERE id='".mysql_real_escape_string($_POST['id'])."'");
			$result=mysql_fetch_array($requete);
			$vignette_orig=$result['vignette_orig'];
			$vignette_mini=$result['vignette_mini'];
			if (strlen($vignette_orig)>3)
				{
				@unlink($chemin_relatif."/upload_galeries/".$vignette_orig);
				@unlink($chemin_relatif."/upload_galeries/".$vignette_mini);
				}
			$requete=requete("UPDATE actualites SET
							 		vignette_orig='".$nomtemporaire."',
									vignette_mini='vignette_".$nomtemporaire."'
								WHERE id='".mysql_real_escape_string($_POST['id'])."'");

			logs($numero_module, $_POST['id'], "Upload d'une vignette d'une actualit&eacute; (".mysql_real_escape_string($_FILES['fichier']["name"]).")");
			affiche_message("La vignette a &eacute;t&eacute; upload&eacute;");
			}
		else
			{
			affiche_message("Le fichier envoy&eacute; n'est pas une image (".$_FILES['fichier']["type"].")");
			}
		}
	else
		{
		affiche_message("Probl&egrave;me lors de l'upload");	
		}

	
	}

if ($_POST['post']==1)
	{
	if ($_REQUEST['id']>0)
		{
		if ($droit_editer==1) 
			{
			// maj	
			$date_diffusion=trans_date($_POST['date_diffusion'],7,"-");
			$date_findiffusion=trans_date($_POST['date_findiffusion'],7,"-");
			
			if ($date_findiffusion=='0000-00-00 00:00:00' or $date_findiffusion=='-- :00') { $date_findiffusion="2999-01-01 00:00:00"; }
			$requete=requete("UPDATE actualites SET
									titre='".mysql_real_escape_string($_POST['titre'])."',
									categorie='".mysql_real_escape_string($_POST['categorie'])."',								
									date_diffusion='".mysql_real_escape_string($date_diffusion)."',
									date_findiffusion='".mysql_real_escape_string($date_findiffusion)."',
									contenu='".mysql_real_escape_string($_POST['contenu'])."',
									actif='".mysql_real_escape_string($_POST['etat'])."',
									imprimer='".mysql_real_escape_string($_POST['imprimer'])."',									
									pdf='".mysql_real_escape_string($_POST['pdf'])."',									
									archive='".mysql_real_escape_string($_POST['archive'])."'
								WHERE id='".mysql_real_escape_string($_REQUEST['id'])."'");
			// log
			logs($numero_module, $_REQUEST['id'], "Modification de l'actualit&eacute; : \"".mysql_real_escape_string($_POST['titre'])."\"");
			}
		}
	else
		{
		if ($droit_ajouter==1)
			{
			// new
			$date_diffusion=trans_date($_POST['date_diffusion'],7,"-");
			$date_findiffusion=trans_date($_POST['date_findiffusion'],7,"-");
			if ($date_findiffusion=='0000-00-00 00:00:00' or $date_findiffusion=='-- :00') { $date_findiffusion="2999-01-01 00:00:00"; }
			$requete=requete("INSERT INTO actualites SET
									titre='".mysql_real_escape_string($_POST['titre'])."',
									categorie='".mysql_real_escape_string($_POST['categorie'])."',
									date_creation=NOW(),
									date_diffusion='".mysql_real_escape_string($date_diffusion)."',
									date_findiffusion='".mysql_real_escape_string($date_findiffusion)."',
									contenu='".mysql_real_escape_string($_POST['contenu'])."',
									actif='".mysql_real_escape_string($_POST['etat'])."',
									pdf='".mysql_real_escape_string($_POST['pdf'])."',
									imprimer='".mysql_real_escape_string($_POST['imprimer'])."',								
									archive='".mysql_real_escape_string($_POST['archive'])."',
									auteur='".$_SESSION['user_id']."'");
			
			$_REQUEST['id']=mysql_insert_id();	
			
			// log			
			logs($numero_module, $_REQUEST['id'], "Cr&eacute;ation de l'actualit&eacute; : \"".mysql_real_escape_string($_POST['titre'])."\"");
			}
			
		}
	affiche_message("L'actualit&eacute; a &eacute;t&eacute; enregistr&eacute; - <a href='index.php?module=actualites&param=actualites&categorie=".$_GET['categorie']."'>retour</a>");
	}


if ($_REQUEST['id']>0)
	{		
	$requete=requete("SELECT * FROM actualites WHERE id='".mysql_real_escape_string($_REQUEST['id'])."'");
	$result=mysql_fetch_array($requete);
	$titre=stripslashes($result['titre']);
	$categorie=$result['categorie'];
	$date_diffusion=substr(trans_date($result['date_diffusion'],5,"-"),0,16);
	$date_findiffusion=substr(trans_date($result['date_findiffusion'],5,'-'),0,16);	
	$contenu=stripslashes($result['contenu']);
	$pdf=$result['pdf'];
	$actif=$result['actif'];
	$imprimer=$result['imprimer'];
	
	$archive=$result['archive'];
	}
else 
	{
	$titre="";	
	}

?>


<fieldset>
  <legend>Ajouter un article</legend>
  <form action="index.php" method="post" name="formulaire" id="formulaire">
    <table width="100%" border="0" cellspacing="0" cellpadding="3">
      <tr>
        <td valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="3">
          <tr>
            <td class="fond0">Titre de l'article :</td>
          </tr>
          <tr>
            <td class="fond3"><input name="titre" type="text" class="formulaire" id="titre" value="<?php echo $titre; ?>" size="60" maxlength="150"></td>
          </tr>
          <tr>
            <td ><table width="100%" border="0" cellspacing="0" cellpadding="1">
              <tr>
                
                <td width="50%" class="fond0">Date de publication :</td>
                <td width="50%" class="fond0">Date de fin de publication :</td>
                </tr>
              <tr>
                
                <td class="fond3">
                  <input name="date_diffusion" type="text" class="formulaire" id="f_date" size="16" maxlength="16" readonly="readonly" value="<?php 
			if (strlen($date_diffusion)>15) { echo $date_diffusion; } else { echo date("d-m-Y H:i"); } ?>" /><script type="text/javascript">   
					Calendar.setup({
						inputField     :    "f_date",   
						ifFormat       :    "%d-%m-%Y %H:%M",      
						showsTime      :    true,
						timeFormat     :    "24"       
								});   
                </script>
                  </td>
                <td class="fond3"><input name="date_findiffusion" type="text" class="formulaire" id="f_findate" size="16" maxlength="16"  value="<?php 
			if (strlen($date_findiffusion)>15) { echo $date_findiffusion; } else { echo ""; } ?>" /><script type="text/javascript">   
					Calendar.setup({
						inputField     :    "f_findate",   
						ifFormat       :    "%d-%m-%Y %H:%M",      
						showsTime      :    true,
						timeFormat     :    "24"       
								});   
                </script> <input name="archive" type="checkbox" id="archive" value="1" <?php if ($archive==1) { echo "checked"; } ?> />
                apr&egrave;s expiration ne pas archiver</td>
                </tr>
              </table></td>
          </tr>
          <tr>
            <td align="right"><table width="100%" border="0" cellspacing="1" cellpadding="0">
              <tr>
                <td width="50%" class="fond0">Cat&eacute;gorie :</td>
                <td class="fond0">&nbsp;</td>
                </tr>
              <tr>
                <td class="fond3"><select name="categorie" class="formulaire" id="categorie">
                  <?php
			 $requete=requete("SELECT id, nom FROM actualites_categories ORDER BY ordre");
			 while($result=mysql_fetch_array($requete))
			 	{
				$id_categorie=$result['id'];
				$nom_categorie=stripslashes($result['nom']);				
				?>
                  <option value="<?php echo $id_categorie; ?>" <?php if ($_REQUEST['id']<1 and $id_categorie==$_GET['categorie']) { echo "selected"; } elseif ($id_categorie==$categorie) { echo "selected"; } ?>><?php echo $nom_categorie; ?></option>
                  <?php					
				}
			 
			 
			 ?>
                  </select></td>
                <td class="fond3"></td>
                </tr>
              <tr>
                <td class="fond0">Derni&egrave;re modification :</td>
                <td class="fond0">
                  <?php
              if ($_REQUEST['id']>0)
				{		  
				$requete_modif=requete("SELECT * FROM logs WHERE module=12 AND type='".mysql_real_escape_string($_REQUEST['id'])."' ORDER BY date DESC LIMIT 0,1");
				if (mysql_num_rows($requete_modif)==1)
					{
					$result_modif=mysql_fetch_array($requete_modif);
					$user_modif=$result_modif['user'];
					$date_modif=$result_modif['date'];					
					echo trans_date($date_modif, 3, "-")." (".id_to_user($user_modif).")";
					}                               
				}
				?>
                  </td>
              </tr>
              </table></td>
          </tr>
        </table>
        <br /></td>
        <td width="200" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="3">
        
          <tr>
            <td class="fond0">Etat :</td>
          </tr>
          <tr>
            <td class="fond3"><select name="etat" class="formulaire" id="etat">
              <option value="0" <?php if ($actif==0) { echo "selected"; } ?>>Non publi&eacute;</option>
              <option value="1" <?php if ($actif==1) { echo "selected"; } ?>>Publi&eacute;</option>
            </select></td>
          </tr>
          <?php if ($parametre['contenu_pdf']==1) { ?><tr>
            <td class="fond0">T&eacute;l&eacute;chargement en PDF :</td>
          </tr>
          <tr>
            <td class="fond3"><input type="radio" name="pdf" id="radio" value="1" <?php if ($pdf==1) { echo 'checked="checked"'; } ?> />
              oui 
              <input name="pdf" type="radio" id="radio2" value="0"  <?php if ($pdf==0 or $pdf=="") { echo 'checked="checked"'; } ?>  /> 
              non</td>
          </tr><?php } 
		   if ($parametre['contenu_print']==1) {
		  
		  ?>
          <tr>
            <td class="fond0">Possibilit&eacute; d'imprimer :</td>
          </tr>
          <tr>
            <td class="fond3"><input type="radio" name="imprimer" id="radio3" value="1" <?php if ($imprimer==1) { echo 'checked="checked"'; } ?> />
oui
  <input name="imprimer" type="radio" id="radio4" value="0"  <?php if ($imprimer==0 or $imprimer=="") { echo 'checked="checked"'; } ?>  />
non</td>
          </tr>
          <?php } 
		 
		  ?>
          
              
        </table></td>
      </tr>
      <tr>
        <td colspan="2" valign="top" class="fond0">Article :</td>
      </tr>
      <tr>
        <td colspan="2" valign="top" class="fond3">
          <?php 
$oFCKeditor = new FCKeditor('contenu') ;
$oFCKeditor->BasePath	= $chemin_site.'/includes/fckeditor/' ;
$oFCKeditor->Config['GoogleMaps_Key']  = $parametre['GoogleMaps_Key'];
$oFCKeditor->Width  = '100%' ;
$oFCKeditor->Height = '500' ;
$oFCKeditor->ToolbarSet = 'Basic';
$oFCKeditor->Value = $contenu;
$oFCKeditor->Create() ;
        ?>
        </td>
      </tr>
      <tr>
        <td colspan="2" valign="top"><table width="100%" border="0" cellspacing="1" cellpadding="3">
          <tr>
            <td><input name="id" type="hidden" id="id" value="<?php echo $_REQUEST['id']; ?>">
              <input name="action" type="hidden" id="action" value="ajout">
              <input name="param" type="hidden" id="param" value="actualitesajout">
              <input name="module" type="hidden" id="module" value="actualites">
              <input name="post" type="hidden" id="post" value="1">
             <input type="image" src="images/bouton_valider.png" alt="Valider" class="formulairebouton"/></td>
            <td align="right">&nbsp;</td>
          </tr>
        </table></td>
      </tr>
    </table>
  </form>
 </fieldset>
<?php
if ($_REQUEST['id']>0 and $droit_editer==1)
		{
?>		
  <form action="index.php" method="post" enctype="multipart/form-data" name="vignette" id="vignette">
    <fieldset>
      <legend>Ajouter une vignette &agrave; l'actualit&eacute;</legend>
      <table width="100%" border="0" cellspacing="0" cellpadding="3">
        <tr>
          <td width="600" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="3">
            <tr>
              <td width="200"  class="fond0">Vignette :</td>
              <td class="fond3"><input type="file" name="fichier" id="fichier" class="formulaire" /></td>
            </tr>
            <tr>
              <td  class="fond0">Taille :</td>
              <td class="fond3"><select name="taille" id="taille"  class="formulaire">
                <?php 
		  $tailles=explode("#",$parametre['tailles_images_plug_fck']);
		  foreach ($tailles as $clef=>$valeur)
		  	{
			?>
                <option value="<?php echo $valeur; ?>"><?php echo str_replace(";"," x ",$valeur); ?></option>
                <?php	
			}
		  ?>
              </select></td>
            </tr>
            <tr>
              <td><input name="id" type="hidden" id="id" value="<?php echo $_REQUEST['id']; ?>" />
                <input name="action" type="hidden" id="action" value="ajout" />
                <input name="param" type="hidden" id="param" value="actualitesajout" />
                <input name="module" type="hidden" id="module" value="actualites" />
              <input name="post" type="hidden" id="post" value="2" /></td>
              <td><input type="image" src="images/bouton_ajouter.png" alt="Valider" class="formulairebouton"/></td>
            </tr>
          </table></td>
          <td valign="top"><?php
          $requete=requete("SELECT vignette_mini FROM actualites WHERE id='".mysql_real_escape_string($_REQUEST['id'])."'");
			$result=mysql_fetch_array($requete);			
			$vignette_mini=$result['vignette_mini'];
			if (strlen($vignette_mini)>3)
				{
				?>
                <img src="upload_galeries/<?php echo $vignette_mini; ?>" /><a href="index.php?module=actualites&categorie=<?php echo $_REQUEST['categorie']; ?>&param=actualitesajout&action=ajout&id=<?php echo $_REQUEST['id']; ?>&post=3"><img src="images/corbeille.gif" width="20" height="20" border="0" /></a>
                <?php	
				}
		  
		  ?></td>
        </tr>
      </table>
    </fieldset>
  </form>

<?php 
		}
?>